import flask
from flask import Flask, render_template, request,redirect,abort,make_response,Response,jsonify,send_file
from common.transaction import ZhiFu
from werkzeug.datastructures import ImmutableMultiDict
from common.mysql import mysql
from config.read_config import congfig_read
import jsonpath
from common.send_request import request_run_main
import json
import redis
from  flask_cors import CORS

app = Flask(__name__)
CORS(app)
# 配置文件路径
config_path = "config\Transaction.ini"
sql_path='sit/mobile-app'
db=mysql(config_path=config_path,sql_path=sql_path)


@app.route("/home")
def home():

        return render_template("lianxi.html")


# @app.route('/counpon',methods=["POST"])
# def counpon():
#     #获取后端数据
#     Phone=flask.request.get_json()
#     scene=int(Phone.get("scene"))
#     amount=Phone.get("amount")

#     #链接数据库
#     sql_path = Phone.get("element") + "/mobile-app"
#     DB=mysql(config_path,sql_path)
#     ecif_text="select ecif_id from t_user where phone_number={}".format(Phone.get("phone"))
#     uid_text="select uid from t_user where phone_number={}".format(Phone.get("phone"))
#     #获取uid, cif
#     cif=DB.select_one(ecif_text)
#     uid=DB.select_one(uid_text)

#     if  cif==None:
#         return flask.jsonify({"code":2005,"message":"号码不存在"})
#     else:
#         # 配置文件section
#         if  scene==1:
#             CouponList_data={"couponStatus": "B", "scene": 10, "couponBusinessType": "L", "prodId": "L01",
#                                "oriAmount": 267300}
#         elif scene==5:
#             CouponList_data={"couponStatus":"B","prodId":"P01","scene":0,"couponBusinessType":"P","vaCode":"9920230907000091101","oriAmount":int(amount)}

#         elif scene==4:
#             CouponList_data={"oriAmount":int(amount),"qrCode":"00020101021126740025ID.CO.BANKNEOCOMMERCE.WWW011893600490594000680102120005500002010303UKE51550025ID.CO.BANKNEOCOMMERCE.WWW0215ID01234567890020303UKE5204573253033605502015802ID5914qris pre0307026012TORAJA UTARA6105918526233052230016485780761020907520703T0163040890",
#                              "merchantName":"qris pre030702","applicableAccount":"A","tipAmount":"0.0",
#                              "merchantNo":"000550000201","aspiMerchantId":"ID0123456789002","couponStatus":"B","prodId":"P02","scene":1,"couponBusinessType":"P"}
#         else:
#             print("无")

#         Life_services = Phone.get("element")+'/Life_services'
#         # 产品请求信息
#         method = congfig_read(config_path).get(Life_services, 'method')
#         content_Type = congfig_read(config_path).get(Life_services, 'content_Type')
#         header = {"uid": str(uid[0]), "Content-Type": "application/json", "app-version": "154",
#                   "device-type": "android", "ecifId": cif[0],
#                   "deviceUuid": "1686813227581-577669-ff6d4863-21ac-4b1f-a6a5-bc5ec3f75370"}
#         # 请求券列表接口并获取券列表
#         CouponList_url = congfig_read(config_path).get(Life_services, 'CouponList_url')

#         CouponList_response = request_run_main(method, content_Type, CouponList_url, header, CouponList_data)
#         print(jsonpath.jsonpath(CouponList_response.json(),'$..data.instantDiscountList')[0])

#         if jsonpath.jsonpath(CouponList_response.json(),'$..data.availableList')[0] == [] :
#              return flask.jsonify({"code":20001,"message":"无可用券"})
#         else:
#              return  flask.jsonify(CouponList_response.json())



# @app.route("/query",methods=["POST"])
# def query():
#     data=request.get_json()
#     phone=data.get("phone")
#     element=data.get("element")
#     coupon=data.get("coupon")
#     scene=data.get("scene")
#     buy_phone=ZhiFu(phone,element,coupon=coupon,scene=scene)
#     respone=buy_phone.PhoneBill()
#     return flask.jsonify(respone)

@app.route("/<pagename>")
def home1(pagename):

        return render_template(pagename)




@app.route("/load_page/ceshi.html")
def load_page(filename):
    # 将请求的文件发送给客户端
    return send_file(filename)




@app.route('/login')
def login():
      return render_template("框体.html")


@app.route('/ceshi',methods=['GET' ,'POST'])
def ceshi():
    
        page1=int(request.form.get('page','1'))
        limit=int(request.form.get('limit','10'))
        offsert=(page1-1)*limit
        text="select uid,customer_name,email,ecif_id,bind_cif_time,create_time from t_user where ecif_id!='' ORDER BY uid desc   limit {},{}".format(offsert,limit)
        result=db.all(text)
        count_text="select count(*) from t_user where ecif_id!='' "
        count=db.all(count_text)
        data=[{"uid":i[0],"customer_name":i[1],"email":i[2],"ecif_id":i[3],"bind_cif_time":i[4],"create_time":i[5]} for i in result]
        message={
                "code":0,
                "msg":"",
                "count":count,
                "data":data
        }
        return jsonify(message)



if __name__ == '__main__':
    app.run(debug=True)

    


